package com.funsport.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.funsport.entity.Permission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;

/**
 * 权限Mapper
 */
@Mapper
public interface PermissionMapper extends BaseMapper<Permission> {
    
    /**
     * 根据角色ID获取权限列表
     */
    @Select("SELECT p.* FROM tb_permission p " +
            "INNER JOIN tb_role_permission rp ON p.id = rp.permission_id " +
            "WHERE rp.role_id = #{roleId}")
    List<Permission> selectByRoleId(Long roleId);
    
    /**
     * 根据用户ID获取权限列表
     */
    @Select("SELECT DISTINCT p.* FROM tb_permission p " +
            "INNER JOIN tb_role_permission rp ON p.id = rp.permission_id " +
            "INNER JOIN tb_user_role ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId}")
    List<Permission> selectByUserId(Long userId);
    
    /**
     * 根据权限编码获取权限
     */
    @Select("SELECT * FROM tb_permission WHERE code = #{code}")
    Permission selectByCode(String code);
    
    /**
     * 获取所有菜单权限（树形结构）
     */
    @Select("SELECT * FROM tb_permission WHERE type = 'menu' ORDER BY sort_order")
    List<Permission> selectMenuPermissions();
}

